package com.df.springmvc.model.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.SN_GUID;
import com.common.util.Date.DateUtil;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.df.springmvc.common.bean.DhtmlxGantBean;
import com.df.springmvc.model.dao.PageDao;
import com.df.springmvc.supper.SupperServices;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class PageServices extends SupperServices{
	@Autowired
	PageDao dao;
	
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		org.apache.log4j.Logger.getLogger(getClass()).info("参数信息:" + paramMap);
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		/**
		 */
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("id","<center>编号</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("code","<center>工号</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("name","<center>名称</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("type","<center>类型</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("tier1","<center>层1</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("tier2","<center>层2</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		attribuate.addFieldBean(new DhtmlGridFieldBean("tier3","<center>层3</center>",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER,DhtmlGridFieldBean.SORT_STR));
		/**
		 * 增加对应的业务代码
		 */
		gridBean.setAttribuate(attribuate);
		ArrayList<Object[]> list=(ArrayList<Object[]>)dao.getList();
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	/**
	 * 这里查询TypeList.由于这个页面为示例数据,就不查询真实数据库
	 * @throws Exception
	 */
	public Object queryType() throws Exception{
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getType();
		return list;
	}
	@Override
	public Object add(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
		// for(String key:paramMap.keySet()){
		// map.put(key,paramMap.get(key));
		// }
		dao.execProc(map);
		map.put("guid",SN_GUID.getUUID());
		map.put("status","1");
		map.put("createby",getCurrentUserBean().getUserId());
		map.put("createtime",new Date());
		map.put("updateby",getCurrentUserBean().getUserId());
		map.put("updatetime",new Date());
		org.apache.log4j.Logger.getLogger(getClass()).info("查询是否存在");
		String id="" + paramMap.get("id");
		if(dao.check(id)){
			dao.add(map);
		}else{
			throw new Exception("已经存在");
		}
		return super.add(paramMap);
	}
	@Override
	public Object upd(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
		map.put("updateby",getCurrentUserBean().getUserId());
		map.put("updatetime",new Date());
		Object rs=dao.upd(map);
		return super.upd(paramMap);
	}
	@Override
	public Object del(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		int successCount=0;
		int errorCount=0;
		for(String item:("" + paramMap.get("guid")).split(",")){
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("guid",item);
			if("0".equals(dao.del(map))){
				errorCount++;
			}else{
				successCount++;
			}
		}
		return "成功[" + successCount + "]失败[" + errorCount + "]";
	}
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		//
		return dao.view(guid);
	}
	public DhtmlxGantBean queryGant(HashMap<String,String> paramMap){
		// TODO Auto-generated method stub
		// 实例化DhtmlxGantBean
		DhtmlxGantBean bean=new DhtmlxGantBean();
		bean.addData("a1",DateUtil.getInstance().setDate(2015,8,21).getDate(),2,"Test1",0.9,1,"0",true);
		bean.addData("a2",DateUtil.getInstance().setDate(2015,8,22).getDate(),2,"Test2",0.9,1,"0",true);
		bean.addData("a3",DateUtil.getInstance().setDate(2015,8,23).getDate(),2,"Test3",0.9,1,"a1",true);
		bean.addData("a4",DateUtil.getInstance().setDate(2015,8,24).getDate(),2,"Test4",0.1,1,"a1",true);
		bean.addData("a5",DateUtil.getInstance().setDate(2015,8,25).getDate(),8,"Test5",0.2,1,"a1",true);
		bean.addData("a6",DateUtil.getInstance().setDate(2015,8,31).getDate(),2,"Test6",0.3,1,"a1",true);
		bean.addData("a7",DateUtil.getInstance().setDate(2015,9,2).getDate(),2,"Test7",0.4,1,"a2",true);
		bean.addData("a8",DateUtil.getInstance().setDate(2015,9,3).getDate(),2,"Test8",0.5,1,"a2",true);
		bean.addData("a9",DateUtil.getInstance().setDate(2015,9,4).getDate(),2,"Test9",0.6,1,"a2",true);
		bean.addLink("1","a4","a5",DhtmlxGantBean.link_start_to_start);
		bean.addLink("2","a4","a6",DhtmlxGantBean.link_start_to_finish);
		bean.addLink("3","a4","a7",DhtmlxGantBean.link_finish_to_start);
		bean.addLink("4","a4","a8",DhtmlxGantBean.link_finish_to_finish);
		return bean;
	}
}
